package com.woniuxy.file.controller;

import com.woniuxy.commons.entity.ResponseResult;
import com.woniuxy.file.utils.AliYunOSSUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private AliYunOSSUtil ossUtil;

    private static final String fileLocation ="classpath:static/images";

    /**
     * 文件上传服务
     * @param name 需要url的服务名字
     * @param file 文件
     * @param request
     * @return
     */
    @PostMapping("/upload")
    public ResponseResult<String> upload(String name, MultipartFile file, HttpServletRequest request){
        String url = ossUtil.uploadFile(file);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (url.equals("图片太大") || url.equals("上传失败") ){
            responseResult.setCode(701);
            responseResult.setMsg(url);
        }else{
            responseResult.setCode(200);
            responseResult.setMsg("success!");
            responseResult.setData(url);
        }
        return responseResult;
    }

}
